const Router = require('koa-router')
const getdata = new Router()
const db = require('../mysql/index');
const bodyparser = require("koa-body");
getdata.use(bodyparser())

const baseurl = 'http://localhost:5000/'
// var gamelist
//
// db.query(`SELECT game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc" FROM game`,function(result){
//     gamelist = result
// })
//根据游戏id获取单个游戏内容
getdata.post('/game', async (ctx)=>{
    let id = ctx.request.body.id
    ctx.body = await new Promise((resolve, reject) => {
        const sql = `SELECT game.G_ID "id", game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc",game.G_URL "gameurl" FROM game WHERE G_ID = ${id} `
        return db.query(sql,[], (data) => {
            data.map(val => {
                val.url = baseurl + val.url
                if(val.gameurl) val.gameurl = baseurl + val.gameurl
            })
            resolve(data)
        })
    })
})
//根据游戏id获取单个游戏评价
getdata.post('/comments', async (ctx)=>{
    let id = ctx.request.body.gameid
    ctx.body = await new Promise((resolve, reject) => {
        const sql = `SELECT * FROM comments c left join USERS u on c.user_id = u.user_id WHERE game_id = ${id};`
        return db.query(sql,[], (data) => {
            data.map(val => {
                if(val.USER_HEADER) val.USER_HEADER = baseurl + val.USER_HEADER
                else val.USER_HEADER = baseurl + 'header/default.png'
            })
            resolve(data)
        })
    })
})
//根据userid获取用户游戏
getdata.post('/mygame', async (ctx)=>{
    let userid = ctx.request.body.userid
    ctx.body = await new Promise((resolve, reject) => {
        const sql = `SELECT * FROM game WHERE G_MAKERID = ${userid}`
        return db.query(sql,[], (data) => {
            data.map(val => {
                if(val.G_COVER) val.G_COVER = baseurl + val.G_COVER
                if(val.G_URL) val.G_URL = baseurl + val.G_URL
            })
            resolve(data)
        })
    })
})

//根据userid获取用户信息
getdata.post('/userinfo', async (ctx)=>{
    let userid = ctx.request.body.userid
    await new Promise((resolve, reject) => {
        const sql = `SELECT * FROM users WHERE USER_ID = ${userid}`
        return db.query(sql,[], (data) => {
            if (!data[0].USER_HEADER){
                data[0].USER_HEADER = 'header/default.png'
            }
            data[0].USER_HEADER = baseurl + data[0].USER_HEADER
            resolve(data)
            ctx.body = {
                userid: data[0].USER_ID,
                username: data[0].USER_NAME,
                header: data[0].USER_HEADER,
                userdesc:  data[0].USER_DESC
            }
        })
    })

})

//根据userid获取用户评论
getdata.post('/usercomments', async (ctx)=>{
    let username = ctx.request.body.username
    ctx.body = await new Promise((resolve, reject) => {
        const sql = `SELECT * FROM comments c left join USERS u on c.user_id = u.user_id WHERE c.user_name = "${username}"`
        return db.query(sql,[], (data) => {
            data.map(val => {
                if(val.USER_HEADER) val.USER_HEADER = baseurl + val.USER_HEADER
                else val.USER_HEADER = baseurl + 'header/default.png'
            })
            resolve(data)
        })
    })
})

module.exports = getdata
